package texture;

import java.lang.reflect.Type;
import java.util.ArrayList;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class AnimationMetadataSectionSerializer extends BaseMetadataSectionSerializer<AnimationMetadataSection> implements JsonSerializer<AnimationMetadataSection> {
	public AnimationMetadataSection deserialize(JsonElement jsonElement, Type par2Type, JsonDeserializationContext par3JsonDeserializationContext) {
		ArrayList<AnimationFrame> arraylist = new ArrayList<>();
		JsonObject jsonobject = (JsonObject) jsonElement;
		int i = this.getIntFromJsonElement(jsonobject.get("frametime"), "frametime", Integer.valueOf(1), 1, Integer.MAX_VALUE);
		int j;

		if (jsonobject.has("frames")) {
			try {
				JsonArray jsonarray = jsonobject.getAsJsonArray("frames");

				for (j = 0; j < jsonarray.size(); ++j) {
					JsonElement jsonelement1 = jsonarray.get(j);
					AnimationFrame animationframe = this.parseAnimationFrame(j, jsonelement1);

					if (animationframe != null) {
						arraylist.add(animationframe);
					}
				}
			} catch (ClassCastException classcastexception) {
				throw new JsonParseException("Invalid animation->frames: expected array, was " + jsonobject.get("frames"), classcastexception);
			}
		}

		int k = this.getIntFromJsonElement(jsonobject.get("width"), "width", Integer.valueOf(-1), 1, Integer.MAX_VALUE);
		j = this.getIntFromJsonElement(jsonobject.get("height"), "height", Integer.valueOf(-1), 1, Integer.MAX_VALUE);
		return new AnimationMetadataSection(arraylist, k, j, i);
	}

	private AnimationFrame parseAnimationFrame(int par1, JsonElement par2JsonElement) {
		if (par2JsonElement.isJsonPrimitive()) {
			try {
				return new AnimationFrame(par2JsonElement.getAsInt());
			} catch (NumberFormatException numberformatexception) {
				throw new JsonParseException("Invalid animation->frames->" + par1 + ": expected number, was " + par2JsonElement, numberformatexception);
			}
		} else if (par2JsonElement.isJsonObject()) {
			JsonObject jsonobject = par2JsonElement.getAsJsonObject();
			int j = this.getIntFromJsonElement(jsonobject.get("time"), "frames->" + par1 + "->time", Integer.valueOf(-1), 1, Integer.MAX_VALUE);
			int k = this.getIntFromJsonElement(jsonobject.get("index"), "frames->" + par1 + "->index", (Integer) null, 0, Integer.MAX_VALUE);
			return new AnimationFrame(k, j);
		} else {
			return null;
		}
	}

	public JsonElement serialize(AnimationMetadataSection par1AnimationMetadataSection, Type par2Type, JsonSerializationContext par3JsonSerializationContext) {
		JsonObject jsonobject = new JsonObject();
		jsonobject.addProperty("frametime", Integer.valueOf(par1AnimationMetadataSection.getFrameTime()));

		if (par1AnimationMetadataSection.getFrameWidth() != -1) {
			jsonobject.addProperty("width", Integer.valueOf(par1AnimationMetadataSection.getFrameWidth()));
		}

		if (par1AnimationMetadataSection.getFrameHeight() != -1) {
			jsonobject.addProperty("height", Integer.valueOf(par1AnimationMetadataSection.getFrameHeight()));
		}

		if (par1AnimationMetadataSection.getFrameCount() > 0) {
			JsonArray jsonarray = new JsonArray();

			for (int i = 0; i < par1AnimationMetadataSection.getFrameCount(); ++i) {
				if (par1AnimationMetadataSection.frameHasTime(i)) {
					JsonObject jsonobject1 = new JsonObject();
					jsonobject1.addProperty("index", Integer.valueOf(par1AnimationMetadataSection.getFrameIndex(i)));
					jsonobject1.addProperty("time", Integer.valueOf(par1AnimationMetadataSection.getFrameTimeSingle(i)));
					jsonarray.add(jsonobject1);
				} else {
					jsonarray.add(new JsonPrimitive(Integer.valueOf(par1AnimationMetadataSection.getFrameIndex(i))));
				}
			}

			jsonobject.add("frames", jsonarray);
		}

		return jsonobject;
	}

	public String getSectionName() {
		return "animation";
	}
}

